package nc.jdbc.framework.util;

import nc.bs.framework.common.InvocationInfoProxy;

public class SQLHelper {
	public static String getInsertSQL(String paramString,
			String[] paramArrayOfString) {
		StringBuffer localStringBuffer = new StringBuffer("INSERT INTO "
				+ paramString + " (");
		for (int i = 0; i < paramArrayOfString.length; ++i) {
			if (paramArrayOfString[i].equalsIgnoreCase("ts"))
				continue;
			localStringBuffer.append(paramArrayOfString[i] + ",");
		}
		localStringBuffer.setLength(localStringBuffer.length() - 1);
		localStringBuffer.append(") VALUES (");
		for (int i = 0; i < paramArrayOfString.length; ++i) {
			if (paramArrayOfString[i].equalsIgnoreCase("ts"))
				continue;
			localStringBuffer.append("?,");
		}
		localStringBuffer.setLength(localStringBuffer.length() - 1);
		localStringBuffer.append(")");
		return localStringBuffer.toString();
	}

	public static String getUpdateSQL(String paramString1,
			String[] paramArrayOfString, String paramString2) {
		StringBuffer localStringBuffer = new StringBuffer("UPDATE "
				+ paramString1 + " SET  ");
		for (int i = 0; i < paramArrayOfString.length; ++i) {
			if (paramArrayOfString[i].equalsIgnoreCase("ts"))
				continue;
			localStringBuffer.append(paramArrayOfString[i] + "=?,");
		}
		localStringBuffer.setLength(localStringBuffer.length() - 1);
		localStringBuffer.append(" WHERE ").append(paramString2).append("=?");
		return localStringBuffer.toString();
	}

	public static String getUpdateSQL(String paramString,
			String[] paramArrayOfString) {
		StringBuffer localStringBuffer = new StringBuffer("UPDATE "
				+ paramString + " SET  ");
		for (int i = 0; i < paramArrayOfString.length; ++i) {
			if (paramArrayOfString[i].equalsIgnoreCase("ts"))
				continue;
			localStringBuffer.append(paramArrayOfString[i] + "=?,");
		}
		localStringBuffer.setLength(localStringBuffer.length() - 1);
		return localStringBuffer.toString();
	}

	public static String getDeleteByPKSQL(String paramString1,
			String paramString2) {
		return "DELETE FROM " + paramString1 + " WHERE " + paramString2 + "=?";
	}

	public static String getDeleteSQL(String paramString,
			String[] paramArrayOfString) {
		StringBuffer localStringBuffer = new StringBuffer("DELETE FROM "
				+ paramString + " WHERE ");

		for (int i = 0; i < paramArrayOfString.length; ++i) {
			localStringBuffer.append(paramArrayOfString[i] + "=? AND ");
		}
		localStringBuffer.setLength(localStringBuffer.length() - 4);
		return localStringBuffer.toString();
	}

	public static String getSelectSQL(String paramString,
			String[] paramArrayOfString1, boolean paramBoolean,
			String[] paramArrayOfString2) {
		StringBuffer localStringBuffer = new StringBuffer();
		if (paramArrayOfString2 == null) {
			localStringBuffer.append("SELECT * FROM " + paramString);
		} else {
			localStringBuffer.append("SELECT ");
			for (int i = 0; i < paramArrayOfString2.length; ++i) {
				localStringBuffer.append(paramArrayOfString2[i] + ",");
			}

			localStringBuffer.setLength(localStringBuffer.length() - 1);
			localStringBuffer.append(" FROM " + paramString);
		}
		String str1 = "AND ";
		if (!(paramBoolean))
			str1 = "OR ";
		if ((paramArrayOfString1 == null) || (paramArrayOfString1.length == 0))
			return localStringBuffer.toString();
		localStringBuffer.append(" WHERE ");
		for (int j = 0; j < paramArrayOfString1.length; ++j) {
			String str2 = paramArrayOfString1[j];
			localStringBuffer.append(str2 + "=? ");
			if (j != paramArrayOfString1.length - 1)
				localStringBuffer.append(str1);
		}
		return localStringBuffer.toString();
	}

	public static String getSelectSQL(String paramString,
			String[] paramArrayOfString) {
		StringBuffer localStringBuffer = new StringBuffer();
		if (paramArrayOfString == null) {
			localStringBuffer.append("SELECT * FROM " + paramString);
		} else {
			localStringBuffer.append("SELECT ");
			for (int i = 0; i < paramArrayOfString.length; ++i) {
				localStringBuffer.append(paramArrayOfString[i] + ",");
			}

			localStringBuffer.setLength(localStringBuffer.length() - 1);
			localStringBuffer.append(" FROM " + paramString);
		}

		return localStringBuffer.toString();
	}

	public static String getCorpPk() {
		String str;
		if (((str = InvocationInfoProxy.getInstance().getCorpCode()) == null)
				|| ((str = str.trim()).equals("")) || (str.equals("null"))) {
			str = "0001";
		}
		return str;
	}

	public static String getSelectSQL(String paramString,
			String[] paramArrayOfString1, String[] paramArrayOfString2) {
		StringBuffer localStringBuffer = new StringBuffer();
		if (paramArrayOfString1 == null) {
			localStringBuffer.append("SELECT * FROM " + paramString);
		} else {
			localStringBuffer.append("SELECT ");
			for (int i = 0; i < paramArrayOfString1.length; ++i) {
				localStringBuffer.append(paramArrayOfString1[i] + ",");
			}

			localStringBuffer.setLength(localStringBuffer.length() - 1);
			localStringBuffer.append(" FROM " + paramString);
		}
		String str1 = "AND ";

		if ((paramArrayOfString2 == null) || (paramArrayOfString2.length == 0))
			return localStringBuffer.toString();
		localStringBuffer.append(" WHERE ");
		for (int j = 0; j < paramArrayOfString2.length; ++j) {
			String str2 = paramArrayOfString2[j];
			localStringBuffer.append(str2 + "=? ");
			if (j != paramArrayOfString2.length - 1)
				localStringBuffer.append(str1);
		}
		return localStringBuffer.toString();
	}
}